package com.ljh.onlinetest.configuration.spring.security;

import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.base.SystemCode;
import com.ljh.onlinetest.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ljh
 * @ClassName RestUtil.java
 * @createTime 2021年12月29日
 */
public class RestUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestUtil.class);

    public static void response(HttpServletResponse response, SystemCode systemCode) {
        response(response, systemCode.getCode(), systemCode.getMessage());
    }

    public static void response(HttpServletResponse response, int systemCode, String msg) {
        response(response, systemCode, msg, null);
    }

    public static void response(HttpServletResponse response,int systemCode,String msg,Object content){
        try {
            RestResponse restResponse = new RestResponse<>(systemCode, msg, content);
            String resStr = JsonUtil.toJsonStr(restResponse);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(resStr);
        }catch (IOException e){
            LOGGER.error(e.getMessage(),e);
        }
    }
}
